package com.heima.behavior.service.impl;
import java.util.Date;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApFollowBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.model.behavior.dtos.FollowBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApFollowBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior> implements ApFollowBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Override
    public ResponseResult saveFollowBehavior(FollowBehaviorDto dto) {
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(dto.getUserId(), null);
        if (apBehaviorEntry==null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"没有对应的实体信息");
        ApFollowBehavior apFollowBehavior = getOne(Wrappers.<ApFollowBehavior>lambdaQuery().eq(ApFollowBehavior::getFollowId, dto.getFollowId())
                .eq(ApFollowBehavior::getEntryId, dto.getUserId()).eq(ApFollowBehavior::getArticleId, dto.getArticleId()));
        if (apFollowBehavior==null){
            apFollowBehavior=new ApFollowBehavior();
            apFollowBehavior.setEntryId(dto.getUserId());
            apFollowBehavior.setArticleId(dto.getArticleId());
            apFollowBehavior.setFollowId(dto.getFollowId());
            apFollowBehavior.setCreatedTime(new Date());
            save(apFollowBehavior);
        }
        return ResponseResult.okResult();
    }
}
